package cn.seaboot.admin.user.service;

import cn.seaboot.admin.user.bean.Role;
import cn.seaboot.admin.user.context.AuthorityCache;
import cn.seaboot.admin.user.context.UserContext;
import cn.seaboot.admin.user.dao.RoleDao;
import cn.seaboot.commons.core.UUIDUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @author postm
 */
@Service
public class RoleService {

    @Resource
    private RoleDao roleDao;

    @Resource
    private UserContext userContext;

    @Resource
    private AuthorityCache<?, ?> authorityCache;

    /**
     * 增
     *
     * @param role pojo
     * @return effect-rows
     */
    public int insert(Role role) {
        role.setId(UUIDUtils.genUUID32());
        role.setDisabled(Boolean.FALSE);
        role.setOrgId(userContext.getOrgId());
        return roleDao.insert(role);
    }

    /**
     * 删
     *
     * @param id id
     * @return effect-rows
     */
    public int deleteById(String id) {
        authorityCache.clear();
        return roleDao.deleteById(userContext.getOrgId(), id);
    }

    /**
     * 改
     *
     * @param id   id
     * @param role pojo
     * @return effect-rows
     */

    public int updateRole(String id, Role role) {
        authorityCache.clear();
        role.setId(id);
        return roleDao.updateSampleById(role);
    }

    /**
     * 查
     *
     * @param id id
     * @return effect-rows
     */

    public Role queryById(String id) {
        return roleDao.selectById(userContext.getOrgId(), id);
    }

    /**
     * 查
     *
     * @param id id
     * @return effect-rows
     */

    public Role queryById(String orgId, String id) {
        return roleDao.selectById(orgId, id);
    }

    /**
     * 增
     *
     * @param params 查询参数
     * @return effect-rows
     */

    public List<Role> queryList(Map<String, Object> params) {
        return roleDao.selectList(params);
    }
}
